import util from "../../../../utils/util";
const app = getApp();
var cityList = [];
Page({
    data: {
        city: [],
    },

    onLoad: function(options) {
        this.index = options.type;
        util.getUrl(app.globalData.url).then(res => {
            this.getCityInfo();
        });
    },

    getCityInfo() {

        let data = {
            action: "huodong",
            type: "city",
        };
        if (this.index == 0) {
            data.chouse = 1;
        } else
            data.chouse = 2;
        util.request("huodong.php", data, "POST").then(res => {
            if (res.data.state == 1) {
                let cityKey = [];
                for (let i = 65; i <= 90; i++)
                    cityKey.push({ key: String.fromCodePoint(i), list: [] })
                cityKey.forEach(item => item.list = res.data.data.city[item.key]);
                cityList = JSON.parse(JSON.stringify(cityKey))
                this.setData({
                    city: cityKey,
                });
            }
        });

    },

    scrollTo(e) {
        let id = util.getValue("id", e);
        ks.pageScrollTo({
            duration: 300,
            selector: `#${id}`,
        });
    },

    chouseCity(e) {
        let citycode = util.getValue("citycode", e);
        let cityname = util.getValue("cityname", e);
        let curPages = getCurrentPages();
        curPages[curPages.length - 2].setData({ citycode, cityname })

        ks.navigateBack({
            delta: 1
        });
    },

    inputTap(e) {
        let city = [{ list: [] }];
        if (e.detail.value)
            cityList.forEach(_city => {
                if (_city.list)
                    Object.assign(city[0].list, _city.list.filter(item => item.cityname.includes(e.detail.value)))
            });
        else
            city = cityList
        this.setData({ city })
    },

    onShareAppMessage: function() {

    }
})